package com.zhangh.summer.admin.support.ds.mysql;


import com.zhangh.summer.admin.support.ds.common.setting.DsSettingProperties;
import com.zhangh.summer.admin.support.multipleds.DataSourceContextHolder;
import com.zhangh.summer.admin.support.multipleds.DatabaseConfig;
import com.zhangh.summer.admin.support.multipleds.IDynamicDataSource;
import com.zhangh.summer.admin.support.multipleds.MultiDsUtil;
import com.zhangh.summer.admin.support.spring.util.SpringBeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

public class MysqlSelector {
    private static final Logger logger= LoggerFactory.getLogger(MysqlSelector.class);
    private static MysqlSelector singleton = null;
    private DsSettingProperties dsSettingProperties;
    private final static Set<String> DS_NAME_SET=new HashSet<>();
    private IDynamicDataSource dynamicDataSource= SpringBeanUtil.getBeanByType(IDynamicDataSource.class);

    public static MysqlSelector getInstance(){
        if(singleton == null){
            synchronized (MysqlSelector.class){
                if(singleton == null){
                    DsSettingProperties dsSettingProperties =SpringBeanUtil.getBeanByType(DsSettingProperties.class);
                    singleton = new MysqlSelector(dsSettingProperties);
                }
            }
        }
        return singleton;
    }
    public MysqlSelector(){

    }
    public MysqlSelector(DsSettingProperties dsSettingProperties){
        this.dsSettingProperties = dsSettingProperties;
    }

    public MybatisMapperFactory ds(){
        return  ds(dsSettingProperties.getDefaultDsName());
    }

    public MybatisMapperFactory ds(String dsName){
        if (StringUtils.isNotBlank(dsName)) {
            if (!DS_NAME_SET.contains(dsName)) {
                synchronized (this){
                    try {
                        dynamicDataSource.addDsByDsName(dsName);
                        DS_NAME_SET.add(dsName);
                    } catch (Exception e) {
                        logger.error("add dsName:{} error, please check properties.",dsName);
                        e.printStackTrace();
                    }
                }
            }
            String dsType = MultiDsUtil.getDsType(dsName);
            if (!dsType.equals(DataSourceContextHolder.getDataSourceType())){
                DataSourceContextHolder.setDataSourceType(dsType);//多数据源切换
            }
            return MybatisMapperFactory.getInstance();
        }else{
            logger.error("dsName can not null! please check method params or ds.setting.default.dsName in properties");
            return null;
        }
    }

    public MybatisMapperFactory ds(DatabaseConfig config){
        if (StringUtils.isNotBlank(config.getDsName())) {
            if (!DS_NAME_SET.contains(config.getDsName())) {
                synchronized (this){
                    try {
                        dynamicDataSource.addDsByDatabaseConfig(config);
                        DS_NAME_SET.add(config.getDsName());
                    } catch (Exception e) {
                        logger.error("add dsName:{} error, please check properties.",config.getDsName());
                        e.printStackTrace();
                    }
                }
            }
            String dsType =config.getDsName(); //MultiDsUtil.getDsType(config.getDsName());
            if (!dsType.equals(DataSourceContextHolder.getDataSourceType())){
                DataSourceContextHolder.setDataSourceType(dsType);//多数据源切换
            }
            return MybatisMapperFactory.getInstance();
        }else{
            logger.error("dsName can not null! please check method params or ds.setting.default.dsName in properties");
            return null;
        }
    }

}
